package com.sebklebes.radar.gae.controller;

import com.sebklebes.radar.gae.converter.RadarConverter;
import com.sebklebes.radar.gae.entity.RadarEntity;
import com.sebklebes.radar.gae.service.RadarService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class HomeController implements Controller {

	private static final Logger log = Logger.getLogger(HomeController.class.getName());
	private RadarService radarService;
	private static final String ADD_RADAR = "add_radar";
	private static final String GET_RADARS = "get_radars";

	public RadarService getRadarService() {
		return radarService;
	}

	public void setRadarService(RadarService radarService) {
		this.radarService = radarService;
	}

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			String stringLatitude = request.getParameter("lat");
			String stringLongitude = request.getParameter("long");
			log.info("lat=" + stringLatitude);
			log.info("long=" + stringLongitude);
			Double latitude = Double.valueOf(stringLatitude);
			Double longitude = Double.valueOf(stringLongitude);

			String action = request.getParameter("action");

			if (action == null || action.isEmpty()) {
				log.info(ADD_RADAR);

				RadarEntity radarEntity = new RadarEntity();
				radarEntity.setLatitude(latitude);
				radarEntity.setLongitude(longitude);
				radarEntity.setLastUpdate(new Date());
				radarEntity.setNumberOfHits(1L);

				radarService.createOrUpdate(radarEntity);
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("radarEntity", radarEntity);

				ModelAndView mav = new ModelAndView("home", params);
				return mav;

			} else if (action.equals(ADD_RADAR)) {
				log.info(ADD_RADAR);

				RadarEntity radarEntity = new RadarEntity();
				radarEntity.setLatitude(latitude);
				radarEntity.setLongitude(longitude);
				radarEntity.setLastUpdate(new Date());
				radarEntity.setNumberOfHits(1L);

				// persist the radar
				RadarEntity savedRadar = radarService.createOrUpdate(radarEntity);

				// send new data back to the client
				String responseString = RadarConverter.convertRadarToString(savedRadar);
				log.info(responseString);

				response.getWriter().write(responseString);
				response.setStatus(HttpServletResponse.SC_OK);

			} else if (action.equals(GET_RADARS)) {
				log.info(GET_RADARS);

				// the client sent the desired range and number of results
				String stringRange = request.getParameter("range");
				String stringMaxNumberOfResults = request.getParameter("maxNrResults");
				Double range = Double.parseDouble(stringRange);
				Double maxNumerOfResults = Double.parseDouble(stringMaxNumberOfResults);

				// get the radars from the database
				List<RadarEntity> radarEntities = radarService.getRadars(latitude, longitude, range, maxNumerOfResults);
				String responseString = RadarConverter.convertRadarArrayToString(radarEntities);

				// send response
				response.getWriter().write(responseString);
				response.setStatus(HttpServletResponse.SC_OK);
			}

			return null;
		} catch (Exception e) {
			return null;
		}
	}
}
